<template>
    <el-form ref="forma" :model="forma" :rules="formaRules" label-position="right" label-width="10rem"
             class="form-width-medium">
        <el-form-item label="账号" prop="name">
            <el-input v-model="forma.name" type="text" placeholder="请输入登陆账号"/>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
            <el-input v-model="forma.pwd" type="text" placeholder="请输入登陆密码"/>
        </el-form-item>
        <!-- <el-form-item label="昵称">
            <el-input v-model="forma.realname" type="text" placeholder="请输入昵称" />
        </el-form-item> -->
        <el-form-item label="手机号" prop="phone">
            <el-input v-model="forma.phone" type="text" placeholder="请输入手机号码"/>
        </el-form-item>
        <el-form-item label="类型" prop="groupid">
            <el-select v-model="forma.groupid" placeholder="角色类型选择">
                <el-option v-for="item in userTypeItems" :key="item.sign" :label="item.name" :value="item.sign"/>
            </el-select>
        </el-form-item>
        <!-- <template v-if="options.mode == 1">
            <el-form-item label="暂存点名称">
                <el-input v-model="forma.name" type="text" placeholder="请输入暂存点名称" />
            </el-form-item>
            <el-form-item label="暂存点位置">
                <el-input v-model="forma.address" type="text" placeholder="请输入暂存点位置" />
            </el-form-item>
        </template> -->
        <el-form-item>
            <el-button @click="eventClickSubmitOperate" :loading="options.loading">保存</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import {Form, FormItem, Input, Button, Message, Select, Option} from 'element-ui';
    import md5 from 'js-md5';

    export default {
        components: {
            [Form.name]: Form, [FormItem.name]: FormItem, [Input.name]: Input, [Button.name]: Button,
            [Select.name]: Select, [Option.name]: Option
        },
        data() {
            return {
                options: {
                    loading: false
                },
                userTypeItems: [
                    {
                        name: '暂存点主管', sign: 3
                    },
                    {
                        name: '平台主管', sign: 4
                    },
                    {
                        name: '库管', sign: 5
                    },
                    {
                        name: '会计', sign: 6
                    }
                ],
                forma: {
                    name: '',
                    pwd: '',
                    phone: '',
                    groupid: '',
                    id: "",
                },
                opwd: "",
                formaRules: {
                    name: [
                        {required: true, message: '请输入登陆账号', trigger: 'blur'}
                    ],
                    pwd: [
                        {required: true, message: '请输入登陆密码', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'}
                    ],
                    groupid: [
                        {required: true, message: '请选择用户类型', trigger: 'blur'}
                    ]
                }
            }
        },
        mounted() {
            let data = JSON.parse(window.localStorage.getItem("rows"));
            console.log(JSON.stringify(data))
            this.forma = {
                name: data.name,
                pwd: data.pwd,
                phone: data.phone,
                id: data.id,
                groupid: data.groupid
            }
            this.opwd = data.pwd;

        },
        methods: {
            eventClickSubmitOperate() {
                this.options.loading = true;

                this.$refs['forma'].validate((valid) => {
                    if (valid) {
                        let url = '/api/home/user/changebyid',
                            params = this.forma;
                        if (this.opwd !== params.pwd) {
                            params.pwd = md5(params.pwd)
                        }
                        params.token = this.$store.state.options.user.token;
                        /*                params.realname = this.forma.phone;
                                        params.zhicheng = this.forma.phone;
                                        params.type = this.forma.groupid;*/

                        this.$util.postRequestInterface(url, params, {
                            success: (res) => {
                                Message.success("用户更新成功");
                                this.$router.go(-1)
                            },
                            complete: (res) => {
                                this.options.loading = false;
                            }
                        });
                    } else {
                        this.options.loading = false;
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
